package com.yanqu.road.dao.impl.activity.langyabang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyabang.data.LangYaBangBeDeclareWarUserData;
import com.yanqu.road.entity.activity.langyabang.data.LangYaBangDeclareWarData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LangYaBangDeclareWarDataDaoImpl extends TempDataObjectDao<LangYaBangDeclareWarData> {
    public static String getLangYaBangDeclareWarDataSql = "select * from `t_c_lang_ya_declare_war` where `activityId`=? and `groupId`=? and `isOver`=0";
    public Map<String, Map<Integer, LangYaBangDeclareWarData>> getNoEndLangYaBangDeclareWarData(int activityId,int groupId) {
        Map<String, Map<Integer, LangYaBangDeclareWarData>> declareWarDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getLangYaBangDeclareWarDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaBangDeclareWarData data = parse(rs);
                declareWarDataMap.putIfAbsent(data.getAttackUnionUid(),new ConcurrentHashMap<>());
                declareWarDataMap.get(data.getAttackUnionUid()).put(data.getDeclareWarUid(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return declareWarDataMap;
    }

    public static String getLangYaBangOverTimeDeclareWarDataSql = "select * from `t_c_lang_ya_declare_war` where `activityId`=? and `groupId`=? and `isOver`=1";
    public Map<String, Map<Integer, LangYaBangDeclareWarData>> getLangYaBangOverTimeDeclareWarData(int activityId,int groupId) {
        Map<String, Map<Integer, LangYaBangDeclareWarData>> declareWarDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getLangYaBangOverTimeDeclareWarDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                LangYaBangDeclareWarData data = parse(rs);
                declareWarDataMap.putIfAbsent(data.getAttackUnionUid(),new ConcurrentHashMap<>());
                declareWarDataMap.get(data.getAttackUnionUid()).put(data.getDeclareWarUid(),data);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return declareWarDataMap;
    }



    public static String updateLangYaBangDeclareWarDataSql = "update `t_c_lang_ya_declare_war` set attackUnionUid=?,defenseUnionUid=?,defenseUnionRank=?,openUserId=?,openUserRemainTimes=?,otherUserRemainTimes=?,isOver=?,beDeclareUserDataMap=?,openTime=? where activityId=? and groupId=? and declareWarUid=?";
    @Override
    protected boolean updateData(LangYaBangDeclareWarData langYaBangDeclareWarData) {
        langYaBangDeclareWarData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, langYaBangDeclareWarData.getAttackUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, langYaBangDeclareWarData.getDefenseUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getDefenseUnionRank()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangDeclareWarData.getOpenUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getOpenUserRemainTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getOtherUserRemainTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getIsOver()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangDeclareWarData.getBeDeclareUserDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT , langYaBangDeclareWarData.getOpenTime()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getDeclareWarUid()));
        if (getDb().executeNoneQuery(updateLangYaBangDeclareWarDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertLangYaBangDeclareWarDataSql = "insert into `t_c_lang_ya_declare_war`(activityId,groupId,declareWarUid,attackUnionUid,defenseUnionUid,defenseUnionRank,openUserId,openUserRemainTimes,otherUserRemainTimes,isOver,beDeclareUserDataMap,openTime) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(LangYaBangDeclareWarData langYaBangDeclareWarData) {
        langYaBangDeclareWarData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getDeclareWarUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, langYaBangDeclareWarData.getAttackUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, langYaBangDeclareWarData.getDefenseUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getDefenseUnionRank()));
        para.put(i++, new DbParameter(Types.BIGINT, langYaBangDeclareWarData.getOpenUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getOpenUserRemainTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getOtherUserRemainTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, langYaBangDeclareWarData.getIsOver()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(langYaBangDeclareWarData.getBeDeclareUserDataMap())));
        para.put(i++, new DbParameter(Types.BIGINT , langYaBangDeclareWarData.getOpenTime()));
        if (getDb().executeNoneQuery(insertLangYaBangDeclareWarDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected LangYaBangDeclareWarData parseData(ResultSet rs) throws SQLException {
        LangYaBangDeclareWarData langYaBangDeclareWarData = new LangYaBangDeclareWarData();
        langYaBangDeclareWarData.setActivityId(rs.getInt("activityId"));
        langYaBangDeclareWarData.setGroupId(rs.getInt("groupId"));
        langYaBangDeclareWarData.setDeclareWarUid(rs.getInt("declareWarUid"));
        langYaBangDeclareWarData.setAttackUnionUid(rs.getString("attackUnionUid"));
        langYaBangDeclareWarData.setDefenseUnionUid(rs.getString("defenseUnionUid"));
        langYaBangDeclareWarData.setDefenseUnionRank(rs.getInt("defenseUnionRank"));
        langYaBangDeclareWarData.setOpenUserId(rs.getLong("openUserId"));
        langYaBangDeclareWarData.setOpenUserRemainTimes(rs.getInt("openUserRemainTimes"));
        langYaBangDeclareWarData.setOtherUserRemainTimes(rs.getInt("otherUserRemainTimes"));
        langYaBangDeclareWarData.setIsOver(rs.getInt("isOver"));
        langYaBangDeclareWarData.setBeDeclareUserDataMap(JSON.parseObject(rs.getString("beDeclareUserDataMap"), new TypeReference<ConcurrentHashMap<Long, LangYaBangBeDeclareWarUserData>>() {}));
        langYaBangDeclareWarData.setOpenTime(rs.getLong("openTime"));
        return langYaBangDeclareWarData;
    }



}
